﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TradeSea
{
    public class Inventory
    {
        public struct Contents
        {
            public string Item;
            public int NumberOfItems;
        }
        List<Contents> Inv = new List<Contents>();
        int balance = 0;

        ItemList items = new ItemList();

        public Inventory()
        {
        }

        public List<Contents> getInventoryList()
        {
            return Inv;
        }

        public int getBalance()
        {
            return balance;
        }

        public void addToInventory(string item)
        {
            if (hasItem(item) == true)
            {
                Contents con = new Contents();
                for (int i = 0; i < Inv.Count(); i++)
                {
                    if (Inv.ElementAt<Contents>(i).Item == item)
                    {
                        con = Inv.ElementAt<Contents>(i);
                        con.NumberOfItems++;
                        Inv.RemoveAt(i);
                        Inv.Add(con);
                        break;
                    }
                }
            }
            else
            {
                Contents thing = new Contents();
                thing.Item = item;
                thing.NumberOfItems = 1;
                Inv.Add(thing);
            }
        }

        public List<string> PrintInventory()
        {
            List<string> tmp = new List<string>();
            tmp.Add("Inventory:");
            foreach (Contents c in Inv)
            {
                tmp.Add(c.Item.PadRight(30) + "x" + c.NumberOfItems.ToString());
            }
            return tmp;
        }

        public List<string> PrintIndividualInventory()
        {
            List<string> tmp = new List<string>();
            foreach (Contents c in Inv)
            {
                int i = c.NumberOfItems;
                while (i != 0)
                {
                    tmp.Add(c.Item);
                    i--;
                }
            }
            return tmp;
        }

        public List<string> PrintInvintoryWOamount()
        {
            List<string> tmp = new List<string>();
            foreach (Contents c in Inv)
            {
                tmp.Add(c.Item);
            }
            return tmp;
        }

        public void removeItem(string item)
        {
            int i = 0;
            foreach (Contents c in Inv)
            {
                if (c.Item == item)
                    break;
                i++;
            }
            if (i != 0)
                Inv.RemoveAt(i);
        }

        public bool hasItem(string item)
        {
            foreach (Contents c in Inv)
            {
                if (c.Item == item)
                    return true;
            }
            return false;
        }

        public void addMoney(int moolah)
        {
            balance += moolah;
        }

        public void takeMoney(int moolah)
        {
            if (HasEnough(moolah) == true)
                balance -= moolah;
        }

        private bool HasEnough(int value)
        {
            if (balance >= value)
                return true;
            return false;
        }
    }
}
